package projectManagement;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import messages.Announcement;
import messages.AnnouncementMessage;

public class TaskGUI extends JPanel implements ActionListener{

	static ArrayList<Announcement> ann;
	static DefaultListModel<String> dfl;
	static JList<String> list;
	String projectName;
	static JPanel taskPanel;
	Announcement allAnnounce;
	JFrame f;
	
	public TaskGUI(ArrayList<Announcement> allAnnouncement,boolean isLeader, String projectName) {
		allAnnounce = allAnnouncement.get(0);
		setPreferredSize(new Dimension(398, 324));
		setLayout(null);
		
		ann = allAnnouncement;
		this.projectName = projectName;
		taskPanel = new JPanel();
		taskPanel.setBorder(new TitledBorder(null, "TASKS", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		taskPanel.setBounds(0, 11, 398, 274);
		taskPanel.setLayout(new BorderLayout(0, 0));
		
		
		dfl = new DefaultListModel<>();
		if(ann.size() != 0){
			for(int i=0; i<ann.size(); i++){
				dfl.addElement(ann.get(i).getTitle());
			}
		}
		
		list = new JList<>();	
		list.setFont(new Font("Vani", Font.PLAIN, 15));
		list.setModel(dfl);
		list.setSelectedIndex(-1);
		list.setVisibleRowCount(0);
		list.setValueIsAdjusting(true);
		list.setLocation(0,0);
		list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		list.putClientProperty( "Quaqua.List.style", "striped");
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(list);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		taskPanel.add(scrollPane, BorderLayout.CENTER);
		
		add(taskPanel);
		
		
		JButton btnView = new JButton("VIEW");
		btnView.setBounds(10, 283, 89, 41);
		btnView.putClientProperty("Quaqua.Button.style", "bevel");
		btnView.addActionListener(this);
		add(btnView);
		
		JButton btnCancel = new JButton("CANCEL");
		btnCancel.setBounds(300, 283, 98, 41);
		btnCancel.putClientProperty("Quaqua.Button.style", "bevel");
		btnCancel.addActionListener(this);
		add(btnCancel);
		
		JButton btnAdd = new JButton("ADD");
		btnAdd.setBounds(103, 283, 67, 41);
		btnAdd.putClientProperty("Quaqua.Button.style", "bevel");
		btnAdd.addActionListener(this);
		btnAdd.setVisible(isLeader);
		add(btnAdd);
		
		JButton btnDel = new JButton("DEL");
		btnDel.setBounds(166, 283, 67, 41);
		btnDel.putClientProperty("Quaqua.Button.style", "bevel");
		btnDel.addActionListener(this);
		btnDel.setVisible(isLeader);
		add(btnDel);
		
		JButton btnEdit = new JButton("EDIT");
		btnEdit.setBounds(228, 283, 67, 41);
		btnEdit.putClientProperty("Quaqua.Button.style", "bevel");
		btnEdit.setVisible(isLeader);
		btnEdit.addActionListener(this);
		add(btnEdit);
		
		f = new JFrame();
		f.getContentPane().add(this);

		f.setVisible(true);
		f.setResizable(false);
		f.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		f.pack();
		f.setLocationRelativeTo(null);
	}

	@Override
	public void actionPerformed(ActionEvent btn) {
		String command = btn.getActionCommand();
		
		if(command.equalsIgnoreCase("ADD")){
			new AnnouncementGUI(projectName);
			
		}
		if(command.equalsIgnoreCase("VIEW")){
			new AnnouncementGUI(ann.get(list.getSelectedIndex()));
		}
		if(command.equalsIgnoreCase("DEL")){
			AnnouncementMessage annMsg = new AnnouncementMessage();
			for(int i=0; i<ann.size(); i++){
				if(ann.get(i).getTitle().equalsIgnoreCase(dfl.getElementAt(list.getSelectedIndex()))){
					annMsg.setAnnouncement(ann.get(i));
					annMsg.setCommand("DEL");
				}
			}
			ann.remove(list.getSelectedIndex());
			taskPanel.revalidate();
			dfl.removeElementAt(list.getSelectedIndex());
			taskPanel.repaint();
			ClientGUI.sendAnnouncementMsg(annMsg);
			
		}
		if(command.equalsIgnoreCase("EDIT")){
			new AnnouncementGUI(ann.get(list.getSelectedIndex()), true);
		}
		if(command.equalsIgnoreCase("CANCEL")){
			f.setVisible(false);
		}
		
	}
	
	public static void updateTable(Announcement announcement){
		taskPanel.revalidate();
		dfl.addElement(announcement.getTitle());
		ann.add(announcement);
		taskPanel.repaint();
	}
	
	public static void updateFromEditable(Announcement announcement){
		ann.set(list.getSelectedIndex(), announcement);
		taskPanel.revalidate();
		dfl.setElementAt(announcement.getTitle(), list.getSelectedIndex());
		taskPanel.repaint();
		
	}
}
